
package myfirstgrailsproject

import sun.security.tools.UserSaveCancelButtonListener;

/**
 * This class is a data-holder - this is what 
 * we store in the database.
 * 
 * @author zach
 *
 */
class Course {

	String title;

	//Define how Courses are displayed for creation
	static constraints = {
		title(unique:true, nullable:false, blank:false);
		professor(display:true, nullable:false, blank:false);
	}

	static hasMany = [enrollments:Enrollment, exercises:Exercise];
	static belongsTo = [professor:User];

	def users(){
		return enrollments.collect{it.user}
	}
	
	ArrayList<User> userlist(){
		ArrayList<User> userlist = new ArrayList<User>();
		if (users() == null) return null;
		for (User u : users()){
			userlist.add(u);
		}
		return userlist;
	}
	

	//Figure out the number of users enrolled in a course
	def usercount(){
		if (users()){
			return users().size()
		}
		else{
			return 0
		}
	}

	String toString(){
		return title
	}

	//static mappedBy = [enrolledUsers:"Course"]
}
